package geektime.spring.springbucks.customerservice;

import java.math.BigDecimal;
import java.net.InetSocketAddress;
import java.net.URI;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.Banner;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

@SpringBootApplication
@Slf4j
public class CustomerServiceApplication implements ApplicationRunner {

  @Autowired
  private RestTemplate restTemplate;

  public static void main(String[] args) {
//    SpringApplication.run(CustomerServiceApplication.class, args);
    new SpringApplicationBuilder().sources(CustomerServiceApplication.class)
        .bannerMode(Banner.Mode.OFF)
        .web(WebApplicationType.NONE)
        .run(args);
  }


  @Bean
  public RestTemplate restTemplate(RestTemplateBuilder builder) {
//		return  new RestTemplate();
//     builder.build();


    return  new RestTemplate(new SimpleClientHttpRequestFactory() {{
      setProxy(new java.net.Proxy(java.net.Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 8888)));
    }});
  }

  @Override
  public void run(ApplicationArguments args) throws Exception {
    URI uri = UriComponentsBuilder
        .fromUriString("http://127.0.0.1:8080/coffee/{id}")
        .build(1);

    ResponseEntity<Coffee> c = restTemplate.getForEntity(uri, Coffee.class);
    log.info("Response Status:{},Response Headers:{}", c.getStatusCode(), c.getHeaders());
    log.info("Coffee {}", c.getBody());

    String coffeeUri = "http://127.0.0.1:8080/coffee/";
    Coffee request = Coffee.builder()
        .name("Americano")
        .price(BigDecimal.valueOf(25.00))
        .build();
    Coffee response = restTemplate.postForObject(coffeeUri, request, Coffee.class);
    log.info("New Coffee:{}", response);

    String s = restTemplate.getForObject(coffeeUri, String.class);
    log.info("String :{}", s);
  }
}